package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.vo.ModulationOutBillDetailVo;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.ModulationOutBillDetailMapper;
import com.canaan.business.domain.bill.ModulationOutBillDetail;
import com.canaan.business.service.bill.IModulationOutBillDetailService;
import com.canaan.common.annotation.TableName;
/**
 * 出库调整单明细Service业务层处理
 *
 * @author canaan
 * @date 2025-03-24
 */
@Service
public class ModulationOutBillDetailServiceImpl implements IModulationOutBillDetailService {
    @Resource
    private ModulationOutBillDetailMapper modulationOutBillDetailMapper;

    /**
     * 查询出库调整单明细
     *
     * @param id 出库调整单明细主键
     * @return 出库调整单明细
     */
    @Override
    public ModulationOutBillDetail selectModulationOutBillDetailById(Long id) {
        return modulationOutBillDetailMapper.selectModulationOutBillDetailById(id);
    }

    @Override
    public List<ModulationOutBillDetailVo> selectModulationOutBillDetailListPageDatas(ModulationOutBillDetail modulationOutBillDetail) {
        return modulationOutBillDetailMapper.selectModulationOutBillDetailListPageDatas(modulationOutBillDetail);
    }

    /**
     * 查询出库调整单明细列表
     *
     * @param modulationOutBillDetail 出库调整单明细
     * @return 出库调整单明细
     */
    @Override
    public List<ModulationOutBillDetail> selectModulationOutBillDetailList(ModulationOutBillDetail modulationOutBillDetail) {
        return modulationOutBillDetailMapper.selectModulationOutBillDetailList(modulationOutBillDetail);
    }

    /**
     * 新增出库调整单明细
     *
     * @param modulationOutBillDetail 出库调整单明细
     * @return 结果
     */
    @Override
    public int insertModulationOutBillDetail(ModulationOutBillDetail modulationOutBillDetail) {
        if(StringUtils.isBlank(modulationOutBillDetail.getNo())){
            modulationOutBillDetail.setNo(TableNoUtil.getNo(ModulationOutBillDetail.class));
        }
        if(StringUtils.isEmpty(modulationOutBillDetail.getCreateBy())){
            modulationOutBillDetail.setCreateBy(SecurityUtils.getUsername());
        }
        modulationOutBillDetail.setCreateTime(DateUtils.getNowDate());
        return modulationOutBillDetailMapper.insertModulationOutBillDetail(modulationOutBillDetail);
    }

    /**
     * 批量新增出库调整单明细
     *
     * @param modulationOutBillDetailList 出库调整单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertModulationOutBillDetail(List<ModulationOutBillDetail> modulationOutBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ModulationOutBillDetail modulationOutBillDetail : modulationOutBillDetailList){
            if(StringUtils.isBlank(modulationOutBillDetail.getNo())){
                modulationOutBillDetail.setNo(TableNoUtil.getNo(ModulationOutBillDetail.class));
            }
            modulationOutBillDetail.setCreateBy(username);
            modulationOutBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<modulationOutBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > modulationOutBillDetailList.size()) {
                endIndex = modulationOutBillDetailList.size();
            }
            insertNum = insertNum + modulationOutBillDetailMapper.batchInsertModulationOutBillDetail(modulationOutBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改出库调整单明细
     *
     * @param modulationOutBillDetail 出库调整单明细
     * @return 结果
     */
    @Override
    public int updateModulationOutBillDetail(ModulationOutBillDetail modulationOutBillDetail) {
        modulationOutBillDetail.setUpdateBy(SecurityUtils.getUsername());
        modulationOutBillDetail.setUpdateTime(DateUtils.getNowDate());
        return modulationOutBillDetailMapper.updateModulationOutBillDetail(modulationOutBillDetail);
    }

    /**
     * 批量修改出库调整单明细
     *
     * @param modulationOutBillDetailList 出库调整单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateModulationOutBillDetail(List<ModulationOutBillDetail> modulationOutBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ModulationOutBillDetail modulationOutBillDetail : modulationOutBillDetailList){
            modulationOutBillDetail.setUpdateBy(username);
            modulationOutBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<modulationOutBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > modulationOutBillDetailList.size()) {
                endIndex = modulationOutBillDetailList.size();
            }
            updateNum = updateNum + modulationOutBillDetailMapper.batchUpdateModulationOutBillDetail(modulationOutBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除出库调整单明细
     *
     * @param ids 需要删除的出库调整单明细主键集合
     * @return 结果
     */
    @Override
    public int deleteModulationOutBillDetailByIds(Long[] ids) {
        return modulationOutBillDetailMapper.deleteModulationOutBillDetailByIds(ids);
    }

    /**
     * 删除出库调整单明细信息
     *
     * @param id 出库调整单明细主键
     * @return 结果
     */
    @Override
    public int deleteModulationOutBillDetailById(Long id) {
        return modulationOutBillDetailMapper.deleteModulationOutBillDetailById(id);
    }

    /**
     * 查询出库调整单明细
     *
     * @param no 编号
     * @return 出库调整单明细
     */
    @Override
    public ModulationOutBillDetail selectModulationOutBillDetailByNo(String no) {
        return modulationOutBillDetailMapper.selectModulationOutBillDetailByNo(no);
    }

    /**
     * 批量删除出库调整单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteModulationOutBillDetailByNos(String[] nos) {
        return modulationOutBillDetailMapper.deleteModulationOutBillDetailByNos(nos);
    }

    /**
     * 删除出库调整单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteModulationOutBillDetailByNo(String no) {
        return modulationOutBillDetailMapper.deleteModulationOutBillDetailByNo(no);
    }
}
